package com.syf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.syf.domain.store.BsSoftwareRelease;
import com.syf.service.BsSoftwareReleaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syf.mapper.BsSoftwareUpdataMapper;
import com.syf.domain.store.BsSoftwareUpdata;
import com.syf.service.BsSoftwareUpdataService;

import java.util.List;

/**
*
*
*@author syf
*@create 2022-02-07 19:09
*/
@Service
public class BsSoftwareUpdataServiceImpl extends ServiceImpl<BsSoftwareUpdataMapper, BsSoftwareUpdata> implements BsSoftwareUpdataService{

    @Autowired
    private BsSoftwareReleaseService bsSoftwareReleaseService;


    /**
     * 上传更新包进行软件升级
     *
     * @param bsSoftwareUpdata
     * @return
     */
    @Override
    public boolean uploadSoftUpdata(BsSoftwareUpdata bsSoftwareUpdata) {
        BsSoftwareRelease bsSoftwareRelease = bsSoftwareReleaseService.selBycode(bsSoftwareUpdata.getSoftwareCode());
        bsSoftwareUpdata.setOldMbSize(bsSoftwareRelease.getMbSize());
        bsSoftwareUpdata.setOldVersion(bsSoftwareRelease.getVersion());
        bsSoftwareUpdata.setStatus(1);  //是否已更新（0未更新(更新包)，1已更新(更新记录)）
        boolean save = save(bsSoftwareUpdata);
        if(save){
            bsSoftwareRelease.setVersion(bsSoftwareUpdata.getNewVersion()); //版本
            bsSoftwareRelease.setMbSize(bsSoftwareUpdata.getNewMbSize()); //大小
            bsSoftwareRelease.setSoftwareUrl(bsSoftwareUpdata.getSoftwareUrl()); //软件安装包
            boolean update = bsSoftwareReleaseService.updateById(bsSoftwareRelease);
            return update;
        }
        return false;
    }


    /**
     * 分页条件查询--更新包(/更新记录)列表
     *
     * @param softwareCode
     * @return
     */
    @Override
    public List<BsSoftwareUpdata> selList(String softwareCode) {
        LambdaQueryWrapper<BsSoftwareUpdata> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BsSoftwareUpdata::getSoftwareCode,softwareCode)
                .orderByDesc(BsSoftwareUpdata::getCreateTime);
        return list(wrapper);
    }
}
